package rsj.service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;

import rsj.dao.RoleInfoDao;
import rsj.db.dbutils.UniqueID;
import common.util.Assert;

import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.aibase.utils.helpers.DateHelper;

public class RoleInfoService {
	
	 private static final RoleInfoService infoService = new RoleInfoService();
	 
	 public static RoleInfoService getInstance(){
	        return infoService;
	    }
	
	private static RoleInfoDao infoDao = RoleInfoDao.getInstance();
	
	public void insertRole(Connection conn,MapNode data) throws SQLException{
			
    	String id = String.valueOf(UniqueID.generateId());
    	String roleName = data.getValue("Role_Name");
    	String funcontent = data.getValue("Role_Funcontent");
    	String status = data.getValue("Role_Status");
    	
    	Date cdate = new Date(new java.util.Date().getTime());
    	
    	
    	Assert.isNotBlank(roleName,"角色名称不能为空");
    	Assert.isNotBlank(status,"角色启停状态不能为空");
    	
    	int count = infoDao.checkRoleName(conn, roleName);
   	
    	Assert.isTrue(count == 0, "角色名称已经存在，不能添加！");
    	//增加角色    	
    	infoDao.insertRole(conn, id, roleName, funcontent, status, cdate, DateHelper.strToTimestamp(DateHelper.getDate(new java.util.Date())));   	
    	
    	//添加角色和菜单id对应关系
    	data.putNode("Role_Id", id);
    	insertRome(conn,data);
	}
	
	public void insertRome(Connection conn,MapNode data) throws SQLException{
		
		
		String menuids = data.getValue("Menu_Ids"); 
		String roleId = data.getValue("Role_Id"); 
		//增加角色和菜单id对应关系
    	if(StringUtils.isBlank(menuids)){
    		return ;
    	}
    	infoDao.insertRome(conn, roleId, menuids);    	
    	
	}
	
	public TableNode findAllRole(Connection conn)throws SQLException{
		return infoDao.findAllRole(conn);
	}
	
	 public MapNode findRoleList(Connection conn,MapNode data) throws SQLException{
		 int size = data.getValueIntDef("Page_Size", 10);
		 int pcount = data.getValueIntDef("Page_Count", 1);
		 return infoDao.findRoleList(conn, size, pcount);
	 }
	 
	 public void UpdateRole(Connection conn,MapNode data) throws SQLException{
		 	String roleName = data.getValue("Role_Name");	    		    	
	    	String funcontent = data.getValue("Role_Funcontent");
	    	String RoleId = data.getValue("Role_Id");
	    	String status = data.getValue("Role_Status");
	    	
	    	Assert.isNotBlank(roleName,"角色名称不能为空");
	    	Assert.isNotBlank(RoleId,"角色Id不能为空");
	    	Assert.isNotBlank(status,"角色启停状态不能为空");
	    	
	    	infoDao.updateRole(conn, RoleId, roleName, funcontent, status, DateHelper.strToTimestamp(DateHelper.getDate(new java.util.Date())));
	    	infoDao.delRome(conn, RoleId);
	    	insertRome(conn,data);    	
	 }
	 
	 public MapNode roleById(Connection conn,MapNode data) throws SQLException{
		 String id = data.getValue("Role_Id");
		 Assert.isNotBlank(id,"角色Id不能为空");
		 
		 return infoDao.roleById(conn, id);
		 
	 }
	 
	 public void updateRoleStatus(Connection conn,MapNode data) throws SQLException{
		 String id = data.getValue("Role_Id");
		 String status = data.getValue("Role_Status");
		 Assert.isNotBlank(id,"角色Id不能为空");
		 Assert.isNotBlank(status,"启停状态不能为空");
		 
		 infoDao.updateRoleStatus(conn, id, status);
	 }

}
